package com.hw.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hw.shortlink.project.dao.entity.LinkAccessStats;
import com.hw.shortlink.project.dto.req.ShortLinkGroupStatsReqDto;
import com.hw.shortlink.project.dto.req.ShortLinkStatsReqDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface LinkAccessStatsMapper extends BaseMapper<LinkAccessStats> {

    /**
     * 记录短链接访问数据
     * @param linkAccessStats
     */
    @Insert("INSERT INTO tb_link_access_stats (full_short_url, date, pv, uv, uip, hour, weekday, create_time, update_time, del_flag) " +
            "VALUES( #{reqDto.fullShortUrl}, #{reqDto.date}, #{reqDto.pv}, #{reqDto.uv}, #{reqDto.uip}, #{reqDto.hour}, #{reqDto.weekday}, NOW(), NOW(), 0) " +
            "ON DUPLICATE KEY UPDATE pv = pv +  #{reqDto.pv}, " +
            "uv = uv + #{reqDto.uv}, " +
            " uip = uip + #{reqDto.uip};")
    void shortLinkAccessStats(@Param("reqDto") LinkAccessStats linkAccessStats);


    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    date, " +
            "    SUM(pv) AS pv, " +
            "    SUM(uv) AS uv, " +
            "    SUM(uip) AS uip " +
            "FROM " +
            "    tb_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{reqDto.fullShortUrl} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "     `date`" +
            "order by date")
    List<LinkAccessStats> listAccessStatsByShortLink(@Param("reqDto") ShortLinkStatsReqDto shortLinkStatsReqDto);

    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    date, " +
            "    SUM(pv) AS pv, " +
            "    SUM(uv) AS uv, " +
            "    SUM(uip) AS uip " +
            "FROM " +
            "    tb_link_access_stats " +
            "WHERE" +
            "     date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    `date`" +
            "order by date")
    List<LinkAccessStats> listAccessStatsByGroup(@Param("reqDto") ShortLinkGroupStatsReqDto shortLinkGroupStatsReqDto);


    /**
     * 查询某个短链接的24小时统计数据
     * @param shortLinkStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    hour, " +
            "    SUM(pv) AS pv " +
            "FROM " +
            "    tb_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{reqDto.fullShortUrl} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid,hour")
    List<LinkAccessStats> listHourStatsByShortLink(@Param("reqDto") ShortLinkStatsReqDto shortLinkStatsReqDto);


    //todo fix
    /**
     * 查询某个分组的24小时统计数据
     * @param shortLinkGroupStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    hour, " +
            "    SUM(pv) AS pv " +
            "FROM " +
            "    tb_link_access_stats " +
            "WHERE " +
            "     gid = #{reqDto.gid} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    hour")
    List<LinkAccessStats> listHourStatsByGroup(@Param("reqDto") ShortLinkGroupStatsReqDto shortLinkGroupStatsReqDto);

    /**
     * 查询短链接的每周访问统计数据
     * @param shortLinkStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    weekday, " +
            "    SUM(pv) AS pv " +
            "FROM " +
            "    tb_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{reqDto.fullShortUrl} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    weekday")
    List<LinkAccessStats> listWeekStatsByShortLink(@Param("reqDto") ShortLinkStatsReqDto shortLinkStatsReqDto);

    //todo fix
    /**
     * 查询分组的每周访问统计数据
     * @param shortLinkGroupStatsReqDto
     * @return
     */
    @Select("SELECT " +
            "    weekday, " +
            "    SUM(pv) AS pv " +
            "FROM " +
            "    tb_link_access_stats " +
            "WHERE "+
            "    gid = #{reqDto.gid} " +
            "    AND date BETWEEN #{reqDto.startDate} and #{reqDto.endDate} " +
            "GROUP BY " +
            "    weekday")
    List<LinkAccessStats> listWeekStatsByGroup(@Param("reqDto")ShortLinkGroupStatsReqDto shortLinkGroupStatsReqDto);

    //todo fix
    /**
     * 批量查询短链接的UvPvUip
     * @param gid
     * @param fullShortUrlList
     * @param isToday
     * @return
     */
    List<LinkAccessStats> listUvPvUip(@Param("gid")String gid,@Param("fullShortUrlList") List<String> fullShortUrlList,@Param("isToday")boolean isToday);



}
